<template>
	<view>
		<div>
			<navitop> 
				<i class="iconfont icon-jiantou-copy" slot="left-slot" @click="outlot"></i>
				<span slot="center-slot">添加</span>
				<!-- <i class="iconfont icon-youcecaidan" slot="right-slot"  @click="tapPopup(data)"></i> -->
			</navitop>
		</div>
		
		<div class="detial" :style='window_height' >
			<div style="background-color: #f0f0f0;" class="public_mag_top"></div>
			<scroll-view scroll-y="true" touch-action=none; :style='window_height'>
				<div class="public_list_out" v-for="(item,index) in dataSorce" >
					<div class="public_list" v-if="item.code == 'crmName'">
						<div class='public_list_left_one'>
							<text v-if="item.onAppFill" style="color: red;">*</text><text>{{item.name}}:</text>
						</div>
						<div class="public_list_right">
							<input type="text"  v-model="item.value" @click="name_dom(item.code)" disabled="true">
						</div>
					</div>
					<div class="public_list" v-if="item.code == 'goingStatus'">
						<div class='public_list_left_one'>
							<text v-if="item.onAppFill" style="color: red;">*</text><text>{{item.name}}:</text>
						</div>
						<div class="public_list_right">
							<input type="text"  v-model="item.value" @click="bus_dom(item.code)" disabled="true">
						</div>
					</div>
					<div v-if="item.onEdit">
					<div class="public_list" v-if=" item.code !== 'projectNo' && item.code !== 'crmName' && item.type == '文本' || item.type == '整数'">
						<div class='public_list_left_one'>
							<text v-if="item.onAppFill" style="color: red;">*</text><text>{{item.name}}:</text>
						</div>
						<div class="public_list_right">
							<input type="text"  v-model="item.value" >
						</div>
					</div>
					
					<div class="public_list" v-else-if="item.type == '下拉'">
						<div class='public_list_left_one'>
							<text v-if="item.onAppFill" style="color: red;">*</text><text>{{item.name}}:</text>
						</div>
						<div class="public_list_right">
							<input type="text"  v-model="item.value" @click="bus_dom(item.code)" disabled="true">
						</div>
					</div>
					
					<div class="public_list" v-else-if="item.type == '搜索选择'">
						<div class='public_list_left_one'>
							<text v-if="item.onAppFill" style="color: red;">*</text><text>{{item.name}}:</text>
						</div>
						<div class="public_list_right">
							<input type="text"  v-model="item.value" @click="serach_choice(item.code)" >
						</div>
					</div>
					
					<div class="public_list" v-else-if="item.type == '区块'" >
						<div class='public_list_left_one'>
							<text v-if="item.onAppFill" style="color: red;">*</text><text>{{item.name}}:</text>
						</div>
						<div class="public_list_right">
							<textarea v-model="item.value" style="border: 1px solid #cacaca;height: 100%;width: 70%;"></textarea>
						</div>
					</div>
			
					<div class="public_list" @click="altime(item.code)" v-else-if="item.type == '时间' || item.type == '日期'">
						<div class='public_list_left_one'>
							<text v-if="item.onAppFill" style="color: red;">*</text><text>开始时间:</text>
						</div>
						<div class="public_list_right">
							<span style="width: 60%;">
								<kxTime style="width: 300rpx;" :date='item.value'  :default_time="1" @rundata='kxdatetime'></kxTime>
							</span>
						</div>
				</div>
				</div>
				</div>
				<br>
				<div :style="heigsdiv"></div>
				<div class="public_button" :style="heigs">
					<div class="public_text" @click="okclick">
						保&nbsp;&nbsp;存
					</div>
				</div><br><br><br><br>
			</scroll-view> 
		</div>
		<!-- 弹窗组件 -->
		<popus ref="alert">
			<alertcenter @row="row" :titles="titles" :rundata="rundata" :dataSorc="datas" ></alertcenter>
		</popus>
		
		<chunLeiPopups v-model="value" :popData="data" @tapPopup="tapPopup" :x="345" :y="95" placement="top-end"></chunLeiPopups>
		
	</view>
</template>

<script>
	import navitop from '@/components/common/navi-top'
	import chunLeiPopups from "@/components/chunLei-popups/chunLei-popups"
	import alertcenter from '@/pages/common/common/alert/alertgx'
	import kxTime from "@/components/kx-datetime/kx-datetime"	
	import popus from '@/uni_modules/uni-popup/components/uni-popup/uni-popup'
	import {business,list,ForSelect,listCompanyActivities,businesssave,disable} from '@/common/api/job/jobapi.js'
	import {user_name_list} from "@/common/api/job/alert.js"
	export default {
		components:{
			navitop,
			alertcenter,
			chunLeiPopups,
			popus,
			kxTime
		},
		props:{
			selid: {
				default: ''
			}
		},
		data(){
			return {
				window_height:'',
				dataSorce: [],
				rundata: [],
				datas: [],
				data: [{title:"删除",code:3,icon: 'icon-shanchu'}],
				date: '', //需要展示的时间数据
				value:false,
				titles: '客户名称',
				heigs:'',
				heigsdiv: 0,
				status: [{name:'计划中',code:'activityState'},{name:'执行中',code:'activityState'},{name: '终止',code:'activityState'},{name: '完成',code:'activityState'}],
				time_code: '',	// 获取选中时间code
				actv_class: [],	// 特色	
				user_name: [],	// 负责人
				
				activeList: []	 ,// 获取活动列表
				userList: []	,// 获取用户列表
				
				public_detals: {}	,
				
				user_uid: ''	// 获取用户id
				
				
			}
		},
		mounted() {
			uni.onKeyboardHeightChange(res => {
				res.height = res.height + 50;
				this.heigsdiv = 'height:' + res.height  + 'px;' ;
				this.heigs = 'height:' + res.height + 'px;' ;
			})
		   const res = uni.getSystemInfoSync();
		   this.window_height = 'height:' + res.windowHeight + 'px;';
			
			
			
			
			// 获取详情
			this.business_datils();
			
			// 获取用户列表
			this.user_list();
			
			// 获取活动列表
			this.active_list();
			
		},
		methods:{
			
			// 关闭窗口
			outlot(){
				this.$emit('close',2)
			},
			
			// 获取时间code
			altime(val){
				this.time_code = val;
			},
			
			// 获取时间
			kxdatetime(e){
				for(let i in this.dataSorce){
					if(this.dataSorce[i].code == this.time_code){
						this.dataSorce[i].value = e.substr(0,10);
					}
				}
			},
			
			// 下拉处理 
			bus_dom(val){
				let rundata = [];
				this.datas = [];
				this.dataSorce.forEach(item =>{
					if(val == item.code){
						rundata = item.val.split(',');
						for(let i in rundata){
							this.datas.push({name:rundata[i],code: val})
							this.titles = item.name
						}
					}
				})
				this.$refs.alert.open('bottom')
			},
			
			// 搜索选择
			serach_choice(val){
				this.datas = [];
				if(val == 'activityId'){
					this.titles = '市场活动';
					for(let i in this.dataSorce){
						if(this.dataSorce[i].code == val){
							for(let i in this.activeList){
								this.datas.push({name:this.activeList[i].title,code: 'activityId',id: this.activeList[i].key})
							}
						}
					}
				}
				this.$refs.alert.open('bottom')
				
			},
			
			
			// 客户名称单独处理
			name_dom(val){
				this.datas = this.userList;
				this.$refs.alert.open('bottom')
			},
			
			// 获取详情
			business_datils(){
				list({source: 'business'}).then((result) => {
					if(result.code == 200){
						if(result.data.defineFields !== '' && result.data.defineFields !== null && result.data.defineFields !== undefined){
							result.data.defineFields = JSON.parse(result.data.defineFields);
						}
						result.data.forEach(item => {
							if(item.dataType == 1){
								this.$set(item,'value', '')
							}
							if(item.dataType == 2){
								if(result.data.defineFields !== '' && result.data.defineFields !== null && result.data.defineFields !== undefined){
									this.$set(item, 'value', result.data.defineFields[item.id])
								}
							}
						});
						this.dataSorce = result.data.splice(1,);
					}
				})
				
			},
			
			// 获取用户列表
			user_list(){
				ForSelect({page:{current: 1, size: 1000},crmTypes: [1, 2]}).then((rows) => {
					if(rows.code == 200){
						for(let i in rows.data.records){
							this.userList.push({name: rows.data.records[i].title,id:rows.data.records[i].key,code: 'crmName'});
						}
					}
				})
			},
			
			// 获取活动列表
			active_list(){
				listCompanyActivities().then((rows) =>{
					if(rows.code == 200){
						this.activeList = rows.data;
					}
				})
			},
			
			// 获取弹窗结果 
			row(val) {
				console.log(val)
				if(val.code == 'crmName'){
					this.user_uid = val.id;
				}
				for(let i in this.dataSorce){
					if(this.dataSorce[i].code == 'activityId'){
						this.dataSorce[i].value = val.id;
					}else {
						if(this.dataSorce[i].code == val.code ){
							this.dataSorce[i].value = val.name;
						}
					}
					
				}
				this.$refs.alert.close()
			},
			
			// 保存 
			 okclick(){
				let pas = {};
				let uname = {};
				let define = '';
				for(let i in this.dataSorce){
					if(this.dataSorce[i].dataType == 1){
						if(this.dataSorce[i].onAppFill){
							if(!this.dataSorce[i].value){
								uni.showToast({
									title: '请输入' + this.dataSorce[i].name,
									icon: 'error'
								})
								return false;
							}else {
								uname[this.dataSorce[i].code] = this.dataSorce[i].value;
							}
						}else{
							uname[this.dataSorce[i].code] = this.dataSorce[i].value;
						}
					}
					if( this.dataSorce[i].dataType == 2){
						define = {[this.dataSorce[i].id]: this.dataSorce[i].value}
					}
				}
				uname['defineFields'] = JSON.stringify(define);
				uname['crmId'] = this.user_uid;
				 businesssave(uname).then((rows)=> {
					 if(rows.code == 200){
						 uni.showToast({ 
							title: '保存成功'
						 })
						 setTimeout(()=>{
							 this.$emit('close',1)
						 },1500)
					 }else{
						 uni.showToast({
							title: rows.message,
							icon: 'error'
						 })
					 }
				 })
			 }
			 
			
		}
	}
	
</script>

<style scoped lang="scss">
.detial {
	// height: 100%;
	padding-top: 95rpx;
	width: 100%;
	background-color: #fff;
	.detial_text{
		div {
			width: 95%;
			display: flex;
			// justify-content: space-between;
			margin: auto;
			height: 90rpx;
			line-height: 89rpx;
			border-bottom: 1px solid #f0f0f0;
		}
		
	}
	.detial_text text{
		// width: 40%;
		text-align: left;
		padding-left: 30rpx;
		padding-right: 10rpx;
		color: #808080;
	}
	.detial_text span{
		// padding-left: 30rpx;
	}
	.detial_text input{
		// height: 90rpx;
		width: 60%;
		border: 1px solid #cacaca;
		border-radius: 4px;
		
	}
	
	input:hover {
			border: 1px solid #1890ff;
	}	
}
.stats {
	width: 200px;
	height: 100rpx;
	z-index: 1;
}
.title {
	margin-top: 1px;
	height: 90rpx;
	background-color: #fff;
	line-height: 90rpx;
	padding-left: 10px;
	border-bottom: 1px solid #f0f0f0;
	border-radius: 4px;
}
.second_list {
	height: 90rpx;
	margin-top: 1px;
	background-color: #fff;
	line-height: 90rpx;
	padding-left: 20px;
	border-radius: 4px;

}
.staff {
	border: 1px solid red;
}
</style>
